package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysConfigDto;
import com.itheima.ydd.dto.SysConfigDtoOther;
import com.itheima.ydd.entity.ChildrenConfig;
import com.itheima.ydd.entity.Config;
import com.itheima.ydd.entity.SysConfig;
import com.itheima.ydd.service.ISysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_config")
@CrossOrigin
@Slf4j
public class SysConfigController {
    @Autowired
    private ISysConfigService iSysConfigService;

    /**
     * 查询系统配置
     *
     * @return
     */
    @GetMapping("/all")
    public R all() {
        LambdaQueryWrapper<SysConfig> lqw = new LambdaQueryWrapper<>();
        // 查询list数据
        List list = new ArrayList();
        // 1 设置基础配置
        Config baseconfig = new Config();
        setList(lqw, baseconfig, "基础配置", "base");
        list.add(baseconfig);
        // 2 设置系统设置
        lqw = new LambdaQueryWrapper<>();
        Config sysconfig = new Config();
        setList(lqw, sysconfig, "系统设置", "system");
        list.add(sysconfig);
        // 3 设置其他设置
        lqw = new LambdaQueryWrapper<>();
        Config customconfig = new Config();
        setList(lqw, customconfig, "其他配置", "custom");
        list.add(customconfig);

        // 查询allData数据
        lqw = new LambdaQueryWrapper<>();
        lqw.notIn(SysConfig::getAttrKey, "site_description");
        List<SysConfig> allDatalist = iSysConfigService.list(lqw);
        List<Object> sysConfigDtos = new ArrayList<>();
        // 单独为是否关闭站点的配置设置配置的值
        for (SysConfig sysConfig : allDatalist) {
            if (sysConfig.getAttrKey().equals("close_website")) {
                // 是关闭站点的配置 使用自定义config类设置config_value值
                SysConfigDtoOther sysConfigDtoOther = new SysConfigDtoOther();
                BeanUtils.copyProperties(sysConfig, sysConfigDtoOther);
                List configValue = new ArrayList();
                Map ymap = new HashMap();
                ymap.put("label", "是");
                ymap.put("value", "1");
                Map nmap = new HashMap();
                nmap.put("label", "否");
                nmap.put("value", "2");
                configValue.add(ymap);
                configValue.add(nmap);
                sysConfigDtoOther.setConfigValue(configValue);
                sysConfigDtos.add(sysConfigDtoOther);
            } else {
                SysConfigDto sysConfigDto = new SysConfigDto();
                BeanUtils.copyProperties(sysConfig, sysConfigDto);
                sysConfigDtos.add(sysConfigDto);
            }
        }

        // 查询configForm的信息
        lqw = new LambdaQueryWrapper<>();
        lqw.notIn(SysConfig::getAttrKey, "site_description");
        List<SysConfig> configFormlist = iSysConfigService.list(lqw);
        Map configFormMap = new HashMap();
        for (SysConfig sysConfig : configFormlist) {
            configFormMap.put(sysConfig.getAttrKey(), sysConfig.getAttrValue());
        }

        // 查询configRules的配置信息
        lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysConfig::getAttrKey, "site_name");
        SysConfig configRules = iSysConfigService.getOne(lqw);
        Map configRulesMap = new HashMap();
        configRulesMap.put("site_name", configRules.getValidator());

        Map map = new HashMap();
        map.put("list", list);
        map.put("allData", sysConfigDtos);
        map.put("configForm", configFormMap);
        map.put("configRules", configRulesMap);
        return new R(map, "", 0);
    }

    @PostMapping(value = "/update")
    public ResultData update(@RequestBody List<SysConfig> list) {
        iSysConfigService.updateBatchById(list);
        return ResultData.ok("", "");
    }

    private void setList(LambdaQueryWrapper<SysConfig> lqw, Config baseconfig, String name, String key) {
        baseconfig.setName(name);
        baseconfig.setKey(key);
        lqw.eq(SysConfig::getTabValue, key);
        // 基础配置的集合
        List<SysConfig> baseList = iSysConfigService.list(lqw);
        System.err.println("baseList = " + baseList);
        lqw.groupBy(SysConfig::getGroupValue);
        // 基础配置下分类集合获取所有的分类
        List<SysConfig> groupvalueList = iSysConfigService.list(lqw);
        System.err.println("groupvalueList = " + groupvalueList);
        List<ChildrenConfig> childrenConfigList = new ArrayList<>();
        for (SysConfig sysConfig : groupvalueList) {
            ChildrenConfig childrenConfig = new ChildrenConfig();
            childrenConfig.setName(sysConfig.getGroupValue());
            lqw = new LambdaQueryWrapper<>();
            lqw.eq(SysConfig::getTabValue, key).eq(SysConfig::getGroupValue, sysConfig.getGroupValue());
            List<SysConfig> childrenList = iSysConfigService.list(lqw);
            System.err.println("childrenList = " + childrenList);
            childrenConfig.setData(childrenList);
            childrenConfigList.add(childrenConfig);
        }
        baseconfig.setChildren(childrenConfigList);
    }

}
